﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_Basics.TMS_cargoowner
{
    public class TMS_cargoownerModel : DapperRepository<TodoAppDbContext>, ITransientDependency, ITMS_cargoownerModel
    {
        public TMS_cargoownerModel(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 货主管理显示
        public async Task<PageResponse<TMS_cargoownerDto>> GetCar( string B_name, string B_phone, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TMS_cargoownerDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_cargoownerModel where 1=1 ";
            if (!string.IsNullOrEmpty(B_name))
            {
                sql += $" and B_name like '%{B_name}%'";
            }
            if (!string.IsNullOrEmpty(B_phone))
            {
                sql += $" and B_phone  like '%{B_phone}%'";
            } 

            var list = await dbConnection.QueryAsync<TMS_cargoownerDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TMS_cargoownerDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }
        #endregion

        #region 货主添加
        public async Task<int> Createcargoow(TMS_cargoownerDto info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_cargoownerModel values ('{info.Id}','{info.B_name}','{info.B_phone}','{info.B_addsname}','{info.B_adds}','{info.B_time}','{info.B_txt}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 货主信息删除
        public async Task<int> Deletecardel(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_cargoownerModel where Id='{Id}'", null,
                 await GetDbTransactionAsync());

        }

        #endregion
    }
}
